package com.zadtek.shopmall.activities;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.HomeBaseBean;
import com.zadtek.shopmall.bean.IndexProductItem;
import com.zadtek.shopmall.bean.OperateResponseBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CollectProductActivity extends AbsListActivity {
    private CollectAdapter collectAdapter;
    private List<IndexProductItem> data = new LinkedList<>();

    private int page =1 ;
    private int pagesize = 10;

    @Override
    protected String getTopTitle() {
        return "收藏的商品";
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        collectAdapter = new CollectAdapter(R.layout.collect_item,data);
        collectAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.iv_btn_delete){
                    deleteRequest(position);
                }
            }
        });
        return collectAdapter;
    }

    private void deleteRequest(final int position) {
        Map<String, Object> param = NetTool.newParams();
        param.put("productid",data.get(position).getProductId());
        NetTool.getApi().delCollectProduct(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean>>() {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean> value) {
                        if(value.getCode() == 0 && value.getData().isSuccess()){
                            ToastUtil.showToast("操作成功！");
                            data.remove(position);
                            collectAdapter.notifyDataSetChanged();
                        }else{
                            ToastUtil.showToast(value.getData().getFailMsg());
                        }
                    }
                });
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getCollectProducts(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<IndexProductItem>>>() {
                    @Override
                    public void onData(BaseResponse<List<IndexProductItem>> value) {
                        if(value.getCode() == 0){
                            if(value.getData().size() != 0){
                                data.addAll(value.getData());
                                collectAdapter.notifyDataSetChanged();
                                collectAdapter.loadMoreComplete();
                                page++;
                            }else{
                                collectAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    public void OnItemClick(int position) {
        HomeBaseBean baseBean = new HomeBaseBean();
        baseBean.setType(Const.PRODUCT);
        baseBean.setId(data.get(position).getProductId());
        Const.jumpHandle(CollectProductActivity.this,baseBean);
    }

    private class CollectAdapter extends BaseQuickAdapter<IndexProductItem,BaseViewHolder>{

        public CollectAdapter(int layoutResId, @Nullable List<IndexProductItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, IndexProductItem item) {
            helper.addOnClickListener(R.id.iv_btn_delete);
            Glide.with(mActivity).load(item.getProductImgUrl()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_product_img));
            helper.setText(R.id.tv_product_desc,item.getProductTitle());
            helper.setText(R.id.tv_product_price,item.getProductPrice());
        }
    }
}
